package com.zxy.controller;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.zxy.utils.ServletUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.UUID;

@WebServlet(value = "/GUIDGenerate",loadOnStartup = 1)
public class GUIDGenerate extends HttpServlet {

    private static Logger logger = LogManager.getLogger(GUIDGenerate.class);

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        Gson gson = new Gson();
        UUID uuid = UUID.randomUUID();
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("uuid", uuid.toString());
        out.write(gson.toJson(jsonObject));
    }
    
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    public void init() throws ServletException {
        ServletUtil.APP_CONTEXT = this.getServletContext().getContextPath();
    }
}
